//检查是否为员工，同时更新用户信息
const app = getApp();
let _isEmployee = '',
  _employeeInfo = {},
  _userInfo = {},
  promises = [],
  isChecking = false,
  _ = require('../Common/underscore.js');
/**
 * force 是否强制刷新
 */
const debounce = _.debounce(f => {
  f();
});
module.exports = (force = false) => {
  let _res,
    _rej,
    promise = new Promise((resovle, reject) => {
      _res = resovle;
      _rej = reject;
    });
  if (_isEmployee === '' || force) {
    promises.push({
      resolve: _res,
      reject: _rej
    });
    debounce(() => {
      let EmpInfo = xhs.getStorageSync('isEmployee');
      isChecking = false;
      let {
        employeeInfo = {},
        userInfo = {},
        isEmployee = false
      } = EmpInfo || {};
      xhs.setStorage({
        key: 'employeeInfo',
        data: employeeInfo
      });
      xhs.setStorage({
        key: 'user',
        data: userInfo
      });
      _isEmployee = isEmployee;
      _employeeInfo = employeeInfo;
      _userInfo = userInfo;
      promises.forEach(p => {
        p.resolve({
          employeeInfo,
          userInfo,
          isEmployee
        });
      });
      promises.length = 0;
      // app.$getData('/user/employee/is-employee').then((data) => {
      //   if (data.errCode == 0) {
      //     isChecking = false;
      //     let { employeeInfo = {}, userInfo = {}, isEmployee = false } = data.data || {};
      //     wx.setStorage({
      //       key: 'employeeInfo',
      //       data: employeeInfo,
      //     })
      //     wx.setStorage({
      //       key: 'user',
      //       data: userInfo,
      //     })
      //     _isEmployee = isEmployee;
      //     _employeeInfo = employeeInfo;
      //     _userInfo = userInfo;
      //     promises.forEach((p) => {
      //       p.resolve({ employeeInfo, userInfo, isEmployee })
      //     })
      //     promises.length = 0;
      //   } else {
      //     promises.forEach((p) => {
      //       p.reject()
      //     })
      //   }
      // }, () => {
      //   promises.forEach((p) => {
      //     p.reject()
      //   })
      // })
    });
  } else {
    _res({
      employeeInfo: _employeeInfo,
      userInfo: _userInfo,
      isEmployee: _isEmployee
    });
  }
  return promise;
};